package com.zhixiang.xaec.service.system.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.xaec.dao.system.DepartmentDao;
import com.zhixiang.xaec.entity.system.Department;
import com.zhixiang.xaec.service.system.DepartmentService;


@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department, Long> implements DepartmentService
{
    @Autowired
    private DepartmentDao dao;
    
    public String saveDept(Department department)
    {
        Long localLong = department.getParentId();
        int i = 0;
        if (localLong.longValue() < 1L)
        {
          localLong = new Long(1L);
        }
        else
        {
          i = (get(localLong)).getDepLevel();
        }
        if (i < 1){
            
            i = 1;
        }
        
        department.setDepLevel(Integer.valueOf(i + 1));
        save(department);
        return null;
    }

    public List<Department> findByParentId(Long id){
       return dao.findByParentId(id);
    }
    
    /**
     * 前台得到tree节点集合
     */
   /* public List<TreeObj> getTreeObj(Long id){
        List<TreeObj> treelist = new ArrayList<TreeObj>();
        
        Department dept = get(id);
        TreeObj tobj= new TreeObj();
        tobj.setId(dept.getDepId().toString());
        tobj.setText(dept.getDepName());
        tobj.setExpanded(true);
        treelist.add(tobj);
        findchildren(tobj);
        return treelist;
    }*/
    
//    private void findchildren(TreeObj treeObj){
//        List<Department> dList = findByParentId(Long.parseLong(treeObj.getId()));
//        if(dList != null) {
//            List<TreeObj> treelist = new ArrayList<TreeObj>();
//            for (int i = 0; i < dList.size(); i++ )
//            {
//                Department department =  dList.get(i);
//                TreeObj tobj= new TreeObj();
//                tobj.setId(department.getDepId().toString());
//                tobj.setText(department.getDepName());
//                List<Department> dd = findByParentId(department.getDepId());
//                if(dd.size()==0){
//                    tobj.setLeaf(true);
//                }
//                treelist.add(tobj);
//            }
//            treeObj.setChildren(treelist);
//            
//            for (int i = 0; i < treelist.size(); i++ )
//            {
//                TreeObj tbj = treelist.get(i);
//                findchildren(tbj);
//            }
//        }
//    }
    
    @Resource(type=DepartmentDao.class)
    public void setBaseDao(BaseDao<Department, Long> dao)
    {
        setDao(dao);
    }
    
}
